package cn.iocoder.yudao.module.voice.cc.mq;

import cn.iocoder.yudao.module.voice.cc.listen.AgentStateListen;
import cn.iocoder.yudao.module.voice.cc.listen.CallCdrListen;
import cn.iocoder.yudao.module.voice.cc.listen.ConfigListen;
import cn.iocoder.yudao.module.voice.cc.mq.message.MessageTypeEnum;
import cn.iocoder.yudao.module.voice.cc.mq.message.RocketMQMessage;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * 广播消息的消费者，真正把消息发送出去
 */
@RocketMQMessageListener( // 重点：添加 @RocketMQMessageListener 注解，声明消费的 topic
        topic = "${yudao.voice.sender-rocketmq.topic}",
        consumerGroup = "${yudao.voice.sender-rocketmq.consumer-group}",
        messageModel = MessageModel.BROADCASTING // 设置为广播模式，保证每个实例都能收到消息
)
@RequiredArgsConstructor
public class RocketMQMessageConsumer implements RocketMQListener<RocketMQMessage> {

    @Autowired
    private CallCdrListen callCdrListen;
    @Autowired
    private AgentStateListen agentStateListen;
    @Autowired
    private ConfigListen configListen;
    @Override
    public void onMessage(RocketMQMessage message) {
       if(Objects.equals(MessageTypeEnum.DEVICE_KEY.getType(),message.getMessageType())){
           callCdrListen.listenCallDevice(message.getMessage());
       }
        if(Objects.equals(MessageTypeEnum.DETAIL_KEY.getType(),message.getMessageType())){
            callCdrListen.listenCallDetailQueue(message.getMessage());
        }
        if(Objects.equals(MessageTypeEnum.CALL_LOG_KEY.getType(),message.getMessageType())){
            callCdrListen.listenCallLog(message.getMessage());
        }
        if(Objects.equals(MessageTypeEnum.AGENT_STATE_KEY.getType(),message.getMessageType())){
            agentStateListen.listenAgentState(message.getMessage());
        }
        if(Objects.equals(MessageTypeEnum.AGENT_LOG_KEY.getType(),message.getMessageType())){
            agentStateListen.listenAgentStateLog(message.getMessage());
        }
        if(Objects.equals(MessageTypeEnum.DEFAULT_KEY.getType(),message.getMessageType())){
            configListen.listenAgentState(message.getMessage());
        }
    }

}
